package com.cloud.animationtestdemo.strage

import android.graphics.Path
import android.graphics.PathMeasure
import kotlin.math.abs

/**
 * @author: njb
 * @date:   2024/12/17 0:17
 * @desc:   贝塞尔曲线策略类
 */
class QuadraticBezierBaseAnimationStrategy(controlX: Float, controlY: Float, endControlX:Float, endControlY:Float) : BaseAnimationStrategy {
    private val path = Path()

    init {
        path.moveTo(0f, 0f)
        path.quadTo(controlX, controlY, endControlX, endControlY)
    }

    override fun getAlphaForProgress(progress: Float): Float {
        val pathMeasure = PathMeasure(path, false)
        val distance = pathMeasure.length * progress
        val pos = FloatArray(2)
        val tan = FloatArray(2)
        pathMeasure.getPosTan(distance, pos, tan)
        return abs(pos[1])
    }
}